package cn.cloudbae.schedule;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;

import cn.cloudbae.kit.PortKit;
import cn.cloudbae.service.JobLifeCycleService;
public class FirstJob implements SimpleJob{

	@Autowired
	private JobLifeCycleService jobLifeCycleService;
	
	@Override
	public void execute(ShardingContext context) {
		String port = String.valueOf(PortKit.getPort()); //获得运行时端口
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String now = sdf.format(new Date());
        
        System.out.println(now + "FirstJob at　" +port + ", " + context.toString());
		
		//模拟任务失败后，起新任务重跑
		long time = System.currentTimeMillis() + 1000 * 30; //10秒后重跑
        sdf = new SimpleDateFormat("HH");
        if(!sdf.format(new Date(time)).equals("23")){//如果一直失败到晚上23点，则发送预警信息（暂时未做）
            //sendWarnMsgService.sendWarnMsg(errorMechants);
            String cron = JobLifeCycleService.getCron(new Date(time));
            String customJobName = "fail_get_public_key_" + time + "_" + context.getShardingItem();
            String desc = "任务：" + context.getJobName()+ "，发生错误，现另起任务重跑失败项";
            jobLifeCycleService.scheludeJob(customJobName,RestartJob.class, new Object [] {}, 1,"0=0",cron , desc);
        }
	}
}
